home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / docs / misc / ConcNews.lha / news / general.programming / comp.lang.c_10209_000011.msg < prev    next >
Encoding:
Internet Message Format  |  1994-11-27  |  2.2 KB

  1. Path: dd.chalmers.se!news.chalmers.se!sunic!pipex!howland.reston.ans.net!agate!ames!olivea!hal.com!parlo.hal.COM!not-for-mail
  2. From: paul@hal.COM (Paul Sander)
  3. Newsgroups: comp.lang.c,comp.std.c
  4. Subject: Re: allocating 0 bytes with malloc()?
  5. Date: 27 Feb 1994 02:37:46 -0800
  6. Organization: HaL Computer Systems, Inc.
  7. Lines: 46
  8. Message-ID: <2kpt5q$6gb@parlo.hal.COM>
  9. References: <1994Feb21.112831.1594@inmos.co.uk> <mmj5ohINNphv@exodus.eng.sun.com> <id.DZ871.EL3@nmti.com> <mn092vINNa0o@exodus.Eng.Sun.COM>
  10. NNTP-Posting-Host: parlo.hal.com
  11. Xref: dd.chalmers.se comp.lang.c:10209 comp.std.c:1104
  12.  
  13. In article <mn092vINNa0o@exodus.Eng.Sun.COM>,
  14. Robert Corbett <corbett@lupa.Eng.Sun.COM> wrote:
  15. >>In article <mmj5ohINNphv@exodus.eng.sun.com>,
  16. >>
  17. >>Non-portable code:
  18. >>
  19. >>    ptr = malloc(size);
  20. >>    if(ptr==0 && size!=0) fail...
  21. >>
  22. >>Portable code:
  23. >>
  24. >>    if(size == 0)
  25. >>        ptr = NULL;
  26. >>    else {
  27. >>        ptr = malloc(size);
  28. >>        if(ptr==0) fail...
  29. >>    }
  30. >>
  31. >>I don't see the problem.
  32. >
  33. >Merriam Webster's Collegiate Dictionary, tenth edition, defines the word
  34. >obstacle as "something that impedes progress or achievement."  Note the
  35. >word "impedes."  An obstacle need not prevent progress or achievement, it
  36. >need only impede it.
  37.  
  38. Most folks I know who are concerned about this issue have this macro in
  39. their portability library:
  40.  
  41. #define myMalloc(x) ( (x) ? malloc(x) : (void*) 0 )
  42.  
  43.  
  44. They don't seem to consider malloc's behavior in this regard to be much
  45. of an obstacle.  And those that don't like their system's implementation
  46. of malloc for whatever reasons replace it anyway; there are lots of free
  47. implementations of it around and it's easy to add one to a link line.
  48.  
  49. I guess I work on a different set of problems than Mr. Corbett; I've
  50. never needed to pass a 0 argument to malloc.  Instead, I've always found
  51. ways to optimize away the need to allocate no memory.  But I would be
  52. interested in reading (via email) about cases where this technique
  53. substantially simplified the code.
  54. -- 
  55. Paul M. Sander  (408) 379-7000  |  "You are in a maze of twisty little
  56. HaL Computer Systems, Inc.      |   methods, all just a little different."
  57. 1315 Dell Avenue                |   
  58. Campbell, CA  95008  USA        |
  59.